package com.tao.aszx.manager.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.tao.aszx.manager.service.SysRoleService;
import com.tao.aszx.model.dto.system.ParcelsDto;
import com.tao.aszx.model.dto.system.SysRole;
import com.tao.aszx.model.dto.system.SysRoleDto;
import com.tao.aszx.model.entity.system.Parcels;
import com.tao.aszx.model.vo.common.Result;
import com.tao.aszx.model.vo.common.ResultCodeEnum;
import com.tao.aszx.model.vo.system.ParcelsVo;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@Tag(name="角色")
@RequestMapping(value = "/admin/system/sysParcels")
public class SysParcelsController {

    @Autowired
    private SysRoleService sysRoleService ;

    @GetMapping("/findByPage/{pageNum}/{pageSize}")
    public Result<PageInfo<ParcelsVo>> findByPage(SysRoleDto sysRoleDto ,
                                                @PathVariable(value = "pageNum") Integer pageNum ,
                                                @PathVariable(value = "pageSize") Integer pageSize
                                                  ) {
        System.out.println(sysRoleDto);

        PageInfo<ParcelsVo> pageInfo = sysRoleService.findByPage(sysRoleDto , pageNum , pageSize) ;
        return Result.build(pageInfo , ResultCodeEnum.SUCCESS) ;
    }

    @PostMapping(value = "/saveSysRole")
    public Result saveSysRole(@RequestBody String parcelsDto) {
        System.out.println(parcelsDto);
        ParcelsDto parcelsDto1 = JSON.parseObject(parcelsDto, ParcelsDto.class);


        int num = sysRoleService.saveSysRole(parcelsDto1);
        if (!(num>0)){
            return Result.build(null,ResultCodeEnum.updata_error);
        }
        return Result.build(null , ResultCodeEnum.SUCCESS) ;
    }

    @PutMapping(value = "/updateSysRole")

    public Result updateSysRole(@RequestBody String parcelsDto) {
            System.out.println(parcelsDto);
            ParcelsDto parcelsDto1 = JSON.parseObject(parcelsDto, ParcelsDto.class);
        sysRoleService.updateSysRole(parcelsDto1) ;


        return Result.build(null , ResultCodeEnum.SUCCESS) ;
    }



    @DeleteMapping(value = "/deleteById/{roleId}")
    public Result deleteById(@PathVariable(value = "roleId") Long roleId) {
        sysRoleService.deleteById(roleId) ;
        return Result.build(null , ResultCodeEnum.SUCCESS) ;
    }

    @GetMapping(value = "/findAllRoles/{userId}")
    public Result<Map<String , Object>> findAllRoles(@PathVariable(value = "userId") Long userId) {
        Map<String, Object> resultMap = sysRoleService.findAllRoles(userId);
        System.out.println(resultMap);
        return Result.build(resultMap , ResultCodeEnum.SUCCESS)  ;
    }

    @GetMapping(value = "/findSysRoleMenuByRoleId/{roleId}")
    public Result<Map<String , Object>> findSysRoleMenuByRoleId(@PathVariable(value = "roleId") Long roleId) {
        Map<String , Object> sysRoleMenuList = sysRoleService.findSysRoleMenuByRoleId(roleId) ;
        return Result.build(sysRoleMenuList , ResultCodeEnum.SUCCESS) ;
    }

}